﻿namespace ufida.u9.ait.openapi
{
    using global::ufida.u9.ait.openapi.utils;
    using System;
	using System.Collections.Generic;
	using System.Text;
    using UFIDA.U9.Base;
    using UFIDA.U9.Base.UserRole;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.MO.MO;
    using UFIDA.U9.MO.SFC.DispatchCO;
    using UFIDA.U9.MO.SFC.DispatchCompleteOrder;
    using UFIDA.U9.MO.SFC.DispatchOrder;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// DispatchCompleteOrderDocSaveSV partial 
    /// </summary>	
	public partial class DispatchCompleteOrderDocSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new DispatchCompleteOrderDocSaveSVImpementStrategy();	
		}		
	}

    #region  implement strategy	
    /// <summary>
    /// Impement Implement
    /// 
    /// </summary>	
    internal partial class DispatchCompleteOrderDocSaveSVImpementStrategy : BaseStrategy
    {
        public DispatchCompleteOrderDocSaveSVImpementStrategy() { }

        public override object Do(object obj)
        {
            DispatchCompleteOrderDocSaveSV bpObj = (DispatchCompleteOrderDocSaveSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CopyOfCommonArchiveDataDTOData> retlist = new List<CopyOfCommonArchiveDataDTOData>();

                foreach (var dto in bpObj.Data)
                {
                    DispatchCompleteOrderDoc doc = DispatchCompleteOrderDoc.Finder.Find($"DocNo='{dto.DocNo}'");
                    if (doc == null)
                    {
                        //新增逻辑
                        doc = DispatchCompleteOrderBase.CreateDoc(dto);
                        //修改单号
                        EntityUtils.UpdateDocNo<DispatchCompleteOrderDoc>(doc.ID, dto.DocNo);
                        //审核
                        doc = DispatchCompleteOrderBase.ApproveDoc(doc);

                        //处理返回
                        retlist.Add(new CopyOfCommonArchiveDataDTOData()
                        {
                            ID = doc.ID,
                            Code = doc.DocNo,
                        });
                    }
                    else
                    {
                        //修改逻辑
                        throw new AITException(599, $"单号:{dto.DocNo}已经存在,不能生成!");
                    }
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {

                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }



        #endregion

    }
	
}